﻿using AbpToDomain.EntityInfo;
using AbpToDomain.View;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace AbpEntityFrameworkcore.DbContext
{
    [ConnectionStringName("Lmsconnstr")]
    public class MyDbContext : AbpDbContext<MyDbContext>
    {

        public DbSet<Box> boxs { get; set; }
        public DbSet<MaterialBase> materialBases { get; set; }
        public DbSet<MaterialOrder> materialOrders { get; set; }
        public DbSet<Warehouse> warehouses { get; set; }
        public DbSet<BoxGrid> boxGrids { get; set; }
        public DbSet<WarehouseLine> warehouseLines { get; set; }
        public DbSet<WorkerOrder> workerOrders { get; set; }
        public DbSet<NeetOutMaterial> neetOutMaterials { get; set; }
        public DbSet<Inlethouse> inlethouses { get; set; }
        public DbSet<InlethouseLine> inlethousesLine { get; set; }
        public DbSet<TaskOrder> taskOrders { get; set; }
        public DbSet<lognote> lognotes { get; set; }
        public DbSet<CusetomOrder> cusetomOrders { get; set; }
        public DbSet<CusetomOrderLine> cusetomOrdersLine { get; set; }
        public DbSet<SowingWall> SowingWalls { get; set; }
        public DbSet<SowingWallLine> SowingWallLines { get; set; }
        public virtual DbSet<View_Materials> viewMaterials { get; set; }
        public virtual DbSet<View_User> viewUser { get; set; }
        public virtual DbSet<View_Box> viewBox { get; set; }
        public virtual DbSet<View_Materials> View_Materials { get; set; }
        public virtual DbSet<View_NeetOutMaterial> View_NeetOutMaterials { get; set; }
        public virtual DbSet<View_InlethouseLine> View_InlethouseLines { get; set; }
        public virtual DbSet<View_TaskOrder> View_TaskOrders { get; set; }
        public virtual DbSet<View_Box_Material> View_Box_Materials { get; set; }
        public virtual DbSet<View_Log> View_Logs { get; set; }
        public virtual DbSet<View_TaskOrder_OrderNo> View_TaskOrder_OrderNos { get; set; }
        public virtual DbSet<View_repeatOrder> View_RepeatOrders { get; set; }



        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {

      
        }
     

     

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<Warehouse>(n =>
            {
                n.ToTable("Warehouse");       
            });
            builder.Entity<Box>(n =>
            {
                n.ToTable("Box");
                n.HasOne<TaskOrder>(o => o.TaskOrder).
                WithOne(d => d.Box).
                HasForeignKey<TaskOrder>(o => o.Id_Box);

            });
            builder.Entity<MaterialBase>(n =>
            {
                n.ToTable("MaterialBase");
                n.HasOne<BoxGrid>(c => c.BoxGrid).WithMany(a => a.MaterialBases);              
            });
            builder.Entity<MaterialOrder>(n =>
            {
                n.ToTable("MaterialOrder");
             
                n.HasMany<MaterialBase>(s => s.materialBases).WithMany(t => t.MaterialOrders).
                UsingEntity(j => j.ToTable("T_MaterialBases_MaterialOrders"));

               
            });
            builder.Entity<BoxGrid>(n =>
            {
                n.ToTable("BoxGrid");
                n.HasOne<Box>(c=>c.TheBox).WithMany(a=>a.BoxGrids).IsRequired();
            });
            builder.Entity<WarehouseLine>(n =>
            {
                n.ToTable("WarehouseLine");
                n.HasOne<Warehouse>(c => c.TheWarehouse).WithMany(a => a.warehouselines).IsRequired();
                n.HasOne<Box>(o => o.Box).WithOne(d => d.WarehouseLine).HasForeignKey<Box>(o => o.Id_WarehouseLine);
            });
            builder.Entity<WorkerOrder>(n =>
            {
                n.ToTable("WorkerOrder");
                n.HasOne<MaterialOrder>(c=>c.TheMaterialOreder).WithMany(a=>a.workerOrders).IsRequired();
            });
            builder.Entity<NeetOutMaterial>(n =>
            {
                n.ToTable("NeetOutMaterial");
                n.HasOne<MaterialBase>(c => c.Material).
                  WithMany(a => a.NeetOutMaterials).IsRequired();

            });
            builder.Entity<Inlethouse>(n =>
            {
                n.ToTable("Inlethouse");
            });
            builder.Entity<InlethouseLine>(n =>
            {
                n.ToTable("InlethouseLine");
                n.HasOne<Inlethouse>(c => c.TheInlethouse).WithMany(a => a.inlethouselines).IsRequired();
                n.HasOne<Box>(o => o.Box).WithOne(d => d.InlethouseLine).HasForeignKey<Box>(o => o.Id_InlethouseLine);      
            });
            builder.Entity<TaskOrder>(n =>
            {
                n.ToTable("TaskOrder");
            });
            builder.Entity<lognote>(n =>
            {
                n.ToTable("lognote");
            });
            builder.Entity<CusetomOrder>(n =>
            {
                n.ToTable("CusetomOrder");
            });
            builder.Entity<CusetomOrderLine>(n =>
            {
                n.ToTable("CusetomOrderLine");
                n.HasOne<CusetomOrder>(c => c.CusetomOrder).WithMany(a => a.cusetomOrderLines).IsRequired();

            });
            builder.Entity<SowingWall>(n =>
            {
                n.ToTable("SowingWall");
            });
            builder.Entity<SowingWallLine>(n =>
            {
                n.ToTable("SowingWallLine");
                n.HasOne<SowingWall>(c => c.SowingWall).
                WithMany(a => a.sowingWallLines).IsRequired();


            });
            //view
            builder.Entity<View_Materials>(n =>
            {
                n.ToView("View_Materials").HasNoKey();
            });
            builder.Entity<View_User>(n =>
            {
                n.ToView("View_User").HasNoKey();
            });
            builder.Entity<View_Box>(n =>
            {
                n.ToView("View_Box").HasNoKey();
            });
            builder.Entity<View_Materials>(n =>
            {
                n.ToView("View_Materials").HasNoKey();
            });
            builder.Entity<View_NeetOutMaterial>(n =>
            {
                n.ToView("View_NeetOutMaterial").HasNoKey();
            });
            builder.Entity<View_InlethouseLine>(n =>
            {
                n.ToView("View_InlethouseLine").HasNoKey();
            });
            builder.Entity<View_TaskOrder>(n =>
            {
                n.ToView("View_TaskOrder").HasNoKey();
            });
            builder.Entity<View_Box_Material>(n =>
            {
                n.ToView("View_Box_Material").HasNoKey();
            });
            builder.Entity<View_Log>(n =>
            {
                n.ToView("View_Log").HasNoKey();
            });
            builder.Entity<View_TaskOrder_OrderNo>(n =>
            {
                n.ToView("View_TaskOrder_OrderNo").HasNoKey();
            });
            builder.Entity<View_repeatOrder>(n =>
            {
                n.ToView("View_repeatOrder").HasNoKey();
            });
        }


    
    }
  

}
